<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue中的路由基础</title>
    <style>
        .router-link-active {
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link to="/home">主页</router-link>
            <router-link to="/news">新闻</router-link>
        </p>

        <div>
            <!-- 路由出口 -->
            <!-- 路由匹配到的组件将渲染在这里 -->
            <router-view></router-view>
        </div>
    </div>
    <template id="home">
        <div>
            <h2>我是主页</h2>
            <router-link to="/home/login">登录</router-link>
            <router-link to="/home/reg">注册</router-link>
            <router-view></router-view>
        </div>
    </template>
    <template id="news">
        <div>
            <h2>我是新闻页</h2>
            <router-link to="/news/detail/001" >查看新闻001</router-link>
            <br>
            <router-link to="/news/detail/002">查看新闻002</router-link>
            <div>
                <router-view v-bind:id="$route.params.id"></router-view>
            </div>
        </div>

    </template>
    <script src="node_modules/vue/dist/vue.js"></script>
    <script src="node_modules/vue-router/dist/vue-router.js"></script>

    <script>
        // 1.创建所要用到的组件
        var Home = Vue.extend({
            template: '#home'
        });
        var News = Vue.extend({
            template: '#news'
        });
        // 2.准备路由
        var router = new VueRouter({
            routes: [
                {
                    path: '/home',
                    component: Home,
                    children: [
                        {
                            path: 'login',
                            component: { template: '<p>我是登录信息</p>' }
                        },
                        {
                            path: 'reg',
                            component: { template: '<p>我是注册信息</p>' }
                        }
                    ]
                },
                {
                    path: '/news',
                    component: News,
                    children: [
                        { 
                            path: 'detail/:id',
                            component: {
                                template:'<div><p>我是新闻{{id}}</p></div>',
                                props: ['id']
                            }
                        }
                    ]
                }
            ]
        });
        //3.创建根组件，并将路由挂载到上面
        var App = new Vue({
            el: "#app",
            router: router
        });

    </script>
</body>

</html>